﻿using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace BusinessLayer
{
    public class ApplicationBusiness
    {
        /// <summary>
        /// 添加ticket
        /// </summary>
        /// <param name="vTicket"></param>
        /// <param name="path"></param>
        /// <param name="operationStatus"></param>
        public static void AddTicket(VTicketRequest vTicket, string[] path, int operationStatus)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                DateTime dt = DateTime.Now;
                
                Ticket ticket = new Ticket
                {
                    ApplicationID = vTicket.ApplicationID,
                    EndUserID = vTicket.EndUserId,
                    IssueDescription = vTicket.IssueDescription,
                    Status = vTicket.Status,
                    SubmiteDate = dt,
                    IsFromWeChat = vTicket.IsFromWeChat,
                    Priority = vTicket.Priority,
                    IsActive = vTicket.IsActive,
                    Email = vTicket.Email,
                    Mobile = vTicket.Mobile
                };
                if (vTicket.IssueType != 0 && vTicket.IssueType != null) ticket.IssueType = vTicket.IssueType;
                entities.Ticket.Add(ticket);
                if (null != path && path.Length >0)
                {
                    foreach (var itemPath in path)
                    {
                        if (null != itemPath)
                        {
                            Image image = new Image() { IsActive = true, Path = itemPath, TicketID = ticket.ID, UploadDate = DateTime.Now };
                            entities.Image.Add(image);
                        }
                    }
                }
                TicketOperationLog operationLog = new TicketOperationLog() { TicketID = ticket.ID, ActorName = TicketBusiness.GetEndUserInfo(vTicket.EndUserId).First().Name, Operation = operationStatus, OperationTime = DateTime.Now };
                foreach (var item in vTicket.FieldData)
                {
                    TicketInputFieldData fieldData = new TicketInputFieldData()
                    {
                        TicketID = ticket.ID,
                        TicketInputFieldID = item.TicketInputFieldID,
                        Value = item.Value
                    };
                    entities.TicketInputFieldData.Add(fieldData);
                }
                entities.TicketOperationLog.Add(operationLog);
                entities.SaveChanges();

            }
        }


        #region ticket已解决状态
        /// <summary>
        /// 跳转评价，ticket状态的改变（需求已变更需修改）
        /// </summary>
        /// <param name="ticketId"></param>
        /// <param name="status">需修改为的状态</param>
        /// <param name="operationStatus">操作日志的状态</param>

        public static void GoToEvaluate(int ticketId, int status, int operationStatus)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var ticketInfo = (from ticket in entities.Ticket
                                  where ticket.ID == ticketId
                                  select ticket).FirstOrDefault();
                if (ticketInfo != null)
                {
                    ticketInfo.Status = status;
                    entities.SaveChanges();
                }
                //TicketOperationLog operationLog = new TicketOperationLog() { TicketID = ticketInfo.ID, ActorName = ticketInfo.ApplicationUserName, Operation = operationStatus, OperationTime = DateTime.Now };
                //entities.TicketOperationLog.Add(operationLog);
                //entities.SaveChanges();
            }



        }
        #endregion 已解决结束



        #region 添加评价
        /// <summary>
        /// 添加评价
        /// </summary>
        /// <param name="ticketId"></param>
        /// <param name="ServiceRate"></param>
        /// <param name="ServiceComments"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static void MakeComments(int ticketId, int ServiceRate, string ServiceComments, int status, int operationStatus)
        {

            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var ticketInfo = (from ticket in entities.Ticket
                                  where ticket.ID == ticketId
                                  select ticket).FirstOrDefault();
                if (ticketInfo != null)
                {
                    ticketInfo.ServiceRate = ServiceRate;
                    ticketInfo.ServiceComments = ServiceComments;
                    ticketInfo.Status = status;
                    ticketInfo.CloseDate = DateTime.Now;
                    entities.SaveChanges();

                }
                TicketOperationLog operationLog = new TicketOperationLog() { TicketID = ticketInfo.ID, ActorName = ticketInfo.EndUser.UserName, Operation = operationStatus, OperationTime = DateTime.Now };
                entities.TicketOperationLog.Add(operationLog);
                entities.SaveChanges();
            }

        }
        #endregion 添加评价结束


       

    }
}
